import request from '@/api/request'
import store from '@/store'
import localStore from '@/utils/localStorage.js'
import route from '@/router'
import { setTimeStamp } from '@/utils/auth.js'
/**
 * 登录接口
 */
const login = (data) => {
  // 保存登录时间
  setTimeStamp()
  return request({
    url: '/user/login',
    method: 'POST',
    data
  })
}
/**
 *
 * @param {*} role  通过角色检目录
 */
const getMenus = (role) => {
  return request({
    url: '/menu/list',
    method: 'get',
    params: { role }
  })
}
const logout = (data) => {
  // 清除token
  store.commit('user/setToken', '')
  // 清除个人信息
  store.commit('user/setUserInfo', {})
  // 清除缓存
  localStore.localClear()
  // 清除当前权限菜单
  store.commit('user/setMenus', [])
  // 推出道login页面
  route.push('/login')
}
const getUserInfo = (data) => {
  return request({
    url: '/backstage/user/login/authority',
    methods: 'POST',
    data
  })
}

export default { login, logout, getUserInfo, getMenus }
